﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.WinUI.Bpt;
using ESOL.WinUI.Finance;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageBusinessList : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private SalesProdPlanApplicationItemEntity mSelectedApplicationItem;
        #endregion

        #region Constructor
        public frmManageBusinessList(MstCustomerEntity customer)
        {
            InitializeComponent();
            txtCustomer.Tag = customer;
            if (customer != null)
            {
                txtCustomer.Text = customer.CustomerName;
            }
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbApplicationType, "AT", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbApplicationOrderStatus, "STATUS", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PASTATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS_ITEM, "PASTATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colAPPLICATION_TYPE, "AT", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);
        }
        private void SetField()
        {

        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND PPA.CUSTOMER_ID = '" + customer.CustomerId + "'";
            }
            if (dtpApplicationDate.EditValue != null)
            {
                condition += " AND PPA.APPLICATION_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpApplicationDateTo.EditValue != null)
            {
                condition += " AND PPA.APPLICATION_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (txtPlanApp.Tag != null && txtPlanApp.Tag.GetType() == typeof(SalesProdPlanApplicationEntity))
            {
                SalesProdPlanApplicationEntity app = (SalesProdPlanApplicationEntity)txtPlanApp.Tag;
                condition += " AND PPA.APPLICATION_ID = '" + app.ApplicationId + "'";
            }
            if (txtProductCode.Tag != null && txtProductCode.Tag.GetType() == typeof(MstProductEntity))
            {
                MstProductEntity product = (MstProductEntity)txtProductCode.Tag;
                condition += " AND PPAI.PRODUCT_ID = '" + product.ProductId + "'";
            }
            if (cmbApplicationType.SelectedValue != null && cmbApplicationType.SelectedValue.ToString().Trim() != "")
            {
                condition += " AND PPA.APPLICATION_TYPE = '" + cmbApplicationType.SelectedValue.ToString().Trim() + "'";
            }
            if (string.IsNullOrEmpty(txtProductSpec.Text) == false)
            {
                condition += " AND PPAI.PRODUCT_SPEC_OUT LIKE '%" + txtProductSpec.Text + "%'";
            }
            if (dtpRequiredDeliveryDate.EditValue != null)
            {
                condition += " AND PPAI.REQUIRED_DELIVERY_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpRequiredDeliveryDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpRequiredDeliveryDateTo.EditValue != null)
            {
                condition += " AND PPAI.REQUIRED_DELIVERY_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpRequiredDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PPA.APPLICATION_CODE DESC";
            DataTable dt = SalesProdPlanApplicationItemBs.GetByCondition(condition, orderBy, rowCount);
            DataColumn dc = new DataColumn("IS_SELECTED", typeof(bool));
            dc.DefaultValue = false;
            dt.Columns.Add(dc);
            //DataTable dtDetail = new DataTable();
            //dtDetail.TableName = "Detail";
            //dtDetail.Columns.Add("");
            //dtDetail.Columns.Add("");
            //dtDetail.Columns.Add("");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt.Copy());
            //ds.Tables.Add(dtDetail.Copy());
            //if (!ds.Relations.Contains("合同明细"))
            //{
            //    ds.Relations.Add("合同明细", ds.Tables[0].Columns["APPLICATION_ITEM_ID"], ds.Tables[1].Columns["APPLICATION_ITEM_ID"], true);
            //}
            gvMain.DataSource = ds.Tables[0];
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedApplicationItem = new SalesProdPlanApplicationItemEntity(dr);
            if ((bool)dr["IS_SELECTED"])
            {
                dr["IS_SELECTED"] = false;
            }
            else
            {
                dr["IS_SELECTED"] = true;
            }
        }

        private List<SalesProdPlanApplicationItemEntity> GetAllBySelected()
        {
            List<SalesProdPlanApplicationItemEntity> list = new List<SalesProdPlanApplicationItemEntity>();
            if (gvMain.DataSource == null)
            {
                return list;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            if (dt.Rows.Count == 0)
            {
                return list;
            }
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"])
                {
                    list.Add(new SalesProdPlanApplicationItemEntity(dr));
                }
            }
            return list;
        }
        #endregion

        #region Button Event
        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedApplicationItem == null)
            {
                MessageBox.Show("请选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SalesProdPlanApplicationEntity app = SalesProdPlanApplicationBs.GetSalesProdPlanApplicationByID(mSelectedApplicationItem.ApplicationId);
            frmMaintainProductPlanApplicationOrder frm = new frmMaintainProductPlanApplicationOrder();
            frm.SelectedProdPlanApplication = app;
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        private void btnQuoteOrder_Click(object sender, EventArgs e)
        {
            //List<SalesProdPlanApplicationItemEntity> list = GetAllBySelected();
            //if (list.Count == 0)
            //{
            //    MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //MstCustomerEntity customer = null;
            //if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            //{
            //    customer = (MstCustomerEntity)txtCustomer.Tag;
            //}
            //frmMaintainSalesQuoteOrder frm = new frmMaintainSalesQuoteOrder(list, customer);
            //frm.ThisActionType = ActionType.New;
            //frm.ShowDialog();
            frmMaintainSalesQuoteOrder frm = new frmMaintainSalesQuoteOrder();
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnProdPlanApplication_Click(object sender, EventArgs e)
        {
            List<SalesProdPlanApplicationItemEntity> list = GetAllBySelected();
            MstCustomerEntity customer = null;
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                customer = (MstCustomerEntity)txtCustomer.Tag;
            }
            frmMaintainProductPlanApplicationOrder frm = new frmMaintainProductPlanApplicationOrder(list, customer);
            frm.ThisActionType = ActionType.New;
            DialogResult dlr = frm.ShowDialog();
            if (dlr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnSalesOrder_Click(object sender, EventArgs e)
        {
            //List<SalesProdPlanApplicationItemEntity> list = GetAllBySelected();
            //if (list.Count == 0)
            //{
            //    MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //MstCustomerEntity customer = null;
            //if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            //{
            //    customer = (MstCustomerEntity)txtCustomer.Tag;
            //}
            //frmMaintainSalesOrder frm = new frmMaintainSalesOrder(list, customer);
            //frm.ThisActionType = ActionType.New;
            //frm.ShowDialog();
            frmMaintainSalesOrder frm = new frmMaintainSalesOrder();
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnDeliveryOrder_Click(object sender, EventArgs e)
        {
            List<SalesProdPlanApplicationItemEntity> list = new List<SalesProdPlanApplicationItemEntity>();
            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                bool selected = Convert.ToBoolean(gridView1.GetDataRow(i)["IS_SELECTED"]);
                if (selected)
                {
                    DataRow dr = gridView1.GetDataRow(i);
                    SalesProdPlanApplicationItemEntity entity = new SalesProdPlanApplicationItemEntity(dr);
                    list.Add(entity);
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<SalesDeliveryOrderEntity> listDelivery = SalesDeliveryOrderBs.GetDeliveryOrderListByApplicationItem(list);
            if (listDelivery != null && listDelivery.Count > 0)
            {
                MessageBox.Show("选中的生产计划申请单中，部分已经开过发货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MstCustomerEntity customer = null;
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                customer = (MstCustomerEntity)txtCustomer.Tag;
            }
            frmMaintainDeliveryOrder frm = new frmMaintainDeliveryOrder(list, customer);
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnReturnOrder_Click(object sender, EventArgs e)
        {
            List<SalesProdPlanApplicationItemEntity> list = new List<SalesProdPlanApplicationItemEntity>();
            string tmpOrderId = "";
            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                bool selected = Convert.ToBoolean(gridView1.GetDataRow(i)["IS_SELECTED"]);
                if (selected)
                {
                    DataRow dr = gridView1.GetDataRow(i);
                    SalesProdPlanApplicationItemEntity entity = new SalesProdPlanApplicationItemEntity(dr);
                    if (tmpOrderId == "")
                    {
                        tmpOrderId = entity.ApplicationId;
                    }
                    else if (tmpOrderId != entity.ApplicationId)
                    {
                        MessageBox.Show("所选退货单明细必须来自同一张生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    list.Add(entity);
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MstCustomerEntity customer = null;
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                customer = (MstCustomerEntity)txtCustomer.Tag;
            }
            frmMaintainReturnOrder frm = new frmMaintainReturnOrder(list, customer);
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindDataGrid();
            }

        }

        //private void btnRegistration_Click(object sender, EventArgs e)
        //{

        //}

        //private void btnSalesFee_Click(object sender, EventArgs e)
        //{
        //    MstCustomerEntity customer = null;
        //    if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
        //    {
        //        customer = (MstCustomerEntity)txtCustomer.Tag;
        //    }
        //    frmMaintainSalesFeeExpenseOrder frm = new frmMaintainSalesFeeExpenseOrder(customer);
        //    frm.ThisActionType = ActionType.New;
        //    frm.ShowDialog();
        //}

        //private void btnTechnic_Click(object sender, EventArgs e)
        //{

        //}

        //private void btnOtherFiles_Click(object sender, EventArgs e)
        //{

        //}

        //private void btnProdProcess_Click(object sender, EventArgs e)
        //{

        //}

        //private void btnMaintainReport_Click(object sender, EventArgs e)
        //{

        //}

        private void btnInvoiceApplication_Click(object sender, EventArgs e)
        {
            #region 老方法，已经不用了
            //List<SalesProdPlanApplicationItemEntity> list = new List<SalesProdPlanApplicationItemEntity>();
            //for (int i = 0; i < gvDetail.Rows.Count; i++)
            //{
            //    bool selected = Convert.ToBoolean(gvDetail.Rows[i].Cells["colSelected"].Value);
            //    if (selected)
            //    {
            //        DataRowView dr = (DataRowView)gvDetail.Rows[i].DataBoundItem;
            //        SalesProdPlanApplicationItemEntity entity = new SalesProdPlanApplicationItemEntity(dr);
            //        list.Add(entity);
            //    }
            //}
            //if (list.Count == 0)
            //{
            //    MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //foreach (SalesProdPlanApplicationItemEntity entity in list)
            //{
            //    if (entity.Status != "PASTATUS07")
            //    {
            //        MessageBox.Show("只有已发货未开票的单据才能开票！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }
            //}
            //List<FinAccountsReceivableItemEntity> listItems = FinAccountsReceivableItemBs.GetReceivableItemByApplicationItem(list);
            //if (listItems != null && listItems.Count > 0)
            //{
            //    MessageBox.Show("选中的生产计划申请单中，部分已经开过发发票！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //MstCustomerEntity customer = null;
            //if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            //{
            //    customer = (MstCustomerEntity)txtCustomer.Tag;
            //}
            //Fin.frmReceivableOrderMaintain frm = new Fin.frmReceivableOrderMaintain(list, customer);
            //frm.ThisActionType = ActionType.New;
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    BindDataGrid();
            //}
            #endregion
            MstCustomerEntity customer = null;
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                customer = (MstCustomerEntity)txtCustomer.Tag;
            }
            frmMaintainInvoicingApplication frm = new frmMaintainInvoicingApplication(null, customer);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void txtPlanApp_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanApplication != null)
            {
                txtPlanApp.Text = frm.SelectedProdPlanApplication.ApplicationCode;
                txtPlanApp.Tag = frm.SelectedProdPlanApplication;
            }
            else
            {
                txtPlanApp.Text = "";
                txtPlanApp.Tag = null;
            }
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                txtProductCode.Text = frm.SelectedProduct.ProductCode;
                txtProductCode.Tag = frm.SelectedProduct;
            }
            else
            {
                txtProductCode.Text = "";
                txtProductCode.Tag = null;
            }
        }
        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (mSelectedApplicationItem == null)
            {
                MessageBox.Show("请选择一个制图任务！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmMaintainBlueprintDetail frm = new frmMaintainBlueprintDetail();
            //frm.SelectDrawBlue =  mSelectedApplicationItem ;
            frm.ShowDialog();
        }
        private void btnExport_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }

        }
        #endregion

       

    }
}